package cz.muni.fi.PA165.musiclibraryweb.signin;

import cz.muni.fi.PA165.musiclibraryweb.song.SongPage;
import cz.muni.fi.PA165.musiclibraryweb.register.Register;
import java.io.Serializable;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;


/**
 * Simple example of a sign in page. Even simpler, as shown in the authentication-2 example, is
 * using the SignInPanel from the auth-role package. Beside that this simple example does not
 * support "rememberMe".
 * 
 */
public class SignIn extends WebPage implements Serializable {

    @SpringBean
    private Label errorMessage;

    public SignIn(PageParameters parameters) {
        super(parameters);
       
        add(new Label("title", new StringResourceModel("title", this, null)));
        
        SignInForm signinform = new SignInForm("signInForm");
        signinform.setParameters(parameters);
        add(signinform);
        errorMessage = new Label("error", "");
        errorMessage.setVisible(false);
        add(errorMessage);
        add(new BookmarkablePageLink("register", Register.class));
    }    
    
    public SignIn(PageParameters parameters,Label label) {
        super(parameters);
        
        add(new Label("title", new StringResourceModel("title", this, null)));
        
        SignInForm signinform = new SignInForm("signInForm");
        signinform.setParameters(parameters);
        add(signinform);
        
       if (label == null) {
            errorMessage = new Label("error", "");
            errorMessage.setVisible(false);
        } else {
            errorMessage = label;
            errorMessage.setVisible(true);
        }
        add(errorMessage);
        add(new BookmarkablePageLink("register", Register.class));
        
    }

    /**
     * Sign in form
     */
    public final class SignInForm extends Form<Void>
    {
        private static final String USERNAME = "username";
        private static final String PASSWORD = "password";
        
        private PageParameters parameters;
        private Label errorMessage;
        
        // El-cheapo model for form
        private final ValueMap properties = new ValueMap();

        /**
         * Constructor
         * 
         * @param id
         *            id of the form component
         */
        public SignInForm(final String id)
        {
            super(id);
            
            // Attach textfield components that edit properties map model
            add(new TextField<String>(USERNAME, new PropertyModel<String>(properties, USERNAME)));
            add(new PasswordTextField(PASSWORD, new PropertyModel<String>(properties, PASSWORD)));
        }

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @Override
        public final void onSubmit()
        {
            // Get session info
            SignInSession session = getMySession();
            

            // Sign the user in
            if (session.signIn(getUsername(), getPassword()))
            {
                //if (!continueToOriginalDestination())
                //{
                    setResponsePage(getApplication().getHomePage());
                //}
            }
            else
            {
                // Get the error message from the properties file associated with the Component
                String errmsg = getString("loginError", null, "Unable to sign you in");
                setResponsePage(new SignIn(parameters, new Label("error", errmsg)));

            }
        }

        /**
         * @return
         */
        private String getPassword()
        {
            return properties.getString(PASSWORD);
        }

        /**
         * @return
         */
        private String getUsername()
        {
            return properties.getString(USERNAME);
        }

        /**
         * @return
         */
        private SignInSession getMySession()
        {
            return (SignInSession)getSession();
        }

        public PageParameters getParameters() {
            return parameters;
        }

        public void setParameters(PageParameters parameters) {
            this.parameters = parameters;
        }
        
        
    }
}
